package com.bakar.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bakar.service.UserService;
import com.bakar.util.BakarConstants;
import com.bakar.util.BakarUtils;
import com.bakar.valueobjects.EmailVO;
import com.bakar.valueobjects.FriendVO;
import com.bakar.valueobjects.UserVO;

@SuppressWarnings("serial")
public class UpdateUserServlet extends HttpServlet {
	//************************* uncomment the code for BLOB STORE **************************************	
	/*private BlobstoreService blobstoreService = BlobstoreServiceFactory
			.getBlobstoreService();
*/
	//************************* uncomment the code for BLOB STORE **************************************	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/plain");

		String currentUser = req.getParameter("currentUser");
		String reqType = req.getParameter("reqType");
		String reqNewUser = req.getParameter("newUser");
		String addType = req.getParameter("addType");
		String image = req.getParameter("image");
		String action = req.getParameter("action");

		if ("ADDFRIEND".equals(reqType)) {
			String subject = currentUser + " would like you to join Bakar";
			String customMessage = req.getParameter("additionalMessage");
			if ("USERNAME".equals(addType)){
					String[] newUsers = reqNewUser.split(";");
					for (String newUser: newUsers){
						UserService.intiateFriendRequest(currentUser, newUser,  subject,  customMessage, true) ;
				}
				
			} else if ("EMAIL".equals(addType)){
				
				String[] newUsers = reqNewUser.split(";");
				for (String receiver: newUsers){
					BakarUtils.sendmail(receiver,  currentUser, subject, customMessage );
					UserService.saveEmail(new EmailVO(currentUser, receiver));
				}
			}
			
		} else if ("ADDIMAGE".equals(reqType)) {
			//String fileSize = req.getHeader("Content-Length");
			//if (100 > Integer.parseInt(fileSize)){
			//************************* uncomment the code for BLOB STORE **************************************	
			/*Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);
				BlobKey blobKey = blobs.get("myfile");
				if (blobKey != null) {
					UserVO user = new UserVO(currentUser); 
					user.setImagePath(blobKey.getKeyString());
					UserService.updateUser(user);
				}*/
			//************************* uncomment the code for BLOB STORE **************************************	
			//} else {
				//resp.setContentType("text");
				//resp.getWriter().print("INVALID_FILE");
				
				//throw new IOException();
			//}
			
			UserVO user = new UserVO(currentUser); 
			user.setImagePath(image);
			UserService.updateUser(user);
			
		} else if ("UPDATEFRNDREQ".equals(reqType)) {
			String status = null;
			if("APPROVE".equals(action)){
				status =BakarConstants.FRND_APPROVED;
			} else {
				status =BakarConstants.FRND_REJ;
			}			
			UserService.updateFriendRequest( reqNewUser,  currentUser,  status);
			
		} else if ("UPDATEDISPNAME".equals(reqType)) {
			String displayName = req.getParameter("displayName");
			UserVO userVO = new UserVO(currentUser);
			userVO.setDisplayName(displayName);
			UserService.updateDisplayName(userVO);
			
		}
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}
	
	

}
